package com.marvels.system.common.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.system.common.dao.SysMenuDao;
import com.marvels.system.common.dto.SysMenuDTO;
import com.marvels.system.common.service.SysMenuService;
import com.marvels.system.permission.dto.SysRoleDTO;
import com.marvels.system.permission.dto.SysRoleMenuDTO;

import cn.hutool.core.util.StrUtil;

@Service
public class SysMenuServiceImpl implements SysMenuService {

	@Autowired
	private SysMenuDao sysMenuDao;
	
	@Override
	public Map<String,List> getSysMenuTree(SysRoleDTO role) throws MarvelsBusinessException {
		Map<String,List> map = new HashMap<String,List>();
		//全部菜单数据
		List<SysMenuDTO> list = sysMenuDao.selectAllMenus();
		//菜单结构树
		List<SysMenuDTO> tree = new ArrayList<SysMenuDTO>();
		//角色菜单关系
		List<SysRoleMenuDTO> roleMenuList = sysMenuDao.selectRoleMenuRelationByRoleCode(role);
		//角色拥有的菜单
		List<SysMenuDTO> roleMenus = new ArrayList<SysMenuDTO>();
		if(CollectionUtils.isNotEmpty(list)){
			int index = 0;
			for(SysMenuDTO menu : list){
				//筛选出所有的一级菜单
				if(StringUtils.equals("-1", menu.getParentMenuCode())){
					index++;
					menu.setMenuRelation(String.valueOf(index));
					//递归结构化子菜单
					List<SysMenuDTO> children = getChildren(menu, list,roleMenuList,roleMenus);
					if(CollectionUtils.isNotEmpty(children)){
						menu.setChildren(children);
					}
					tree.add(menu);
				}
			}
		}
		map.put("menuTree",tree);
		map.put("roleMenus",roleMenus);
		return map;
	}
	
	private List<SysMenuDTO> getChildren(SysMenuDTO parent, List<SysMenuDTO> list,List<SysRoleMenuDTO> roleMenuList,List<SysMenuDTO> roleMenus){
		List<SysMenuDTO> children = new ArrayList<SysMenuDTO>();
		int index = 0;
		for(SysMenuDTO node : list){
			//筛选出父级菜单下的全部子菜单
			if(StringUtils.equals(parent.getMenuCode(), node.getParentMenuCode())){
				index++;
				node.setMenuRelation(parent.getMenuRelation()+"-"+index);
				children.add(node);
				//筛选出满足角色菜单关系的全部子菜单
				for(SysRoleMenuDTO roleMenu : roleMenuList){
					if(StringUtils.equals(roleMenu.getMenuCode(), node.getMenuCode())){
						roleMenus.add(node);
					}
				}
			}
		}
		if(CollectionUtils.isNotEmpty(children)){
			for(SysMenuDTO child : children){
				child.setChildren(getChildren(child, list,roleMenuList,roleMenus));
			}
		}
		if(CollectionUtils.isNotEmpty(children)){
			return children;
		}else{
			return null;
		}
	}

	@Override
	public List<SysMenuDTO> getSysRoleMenuTree() throws MarvelsBusinessException {
		UserDTO user = CacheUtils.getCacheUser();
		String userCode = user.getUserCode();
		//角色菜单数据
		List<SysMenuDTO> list = sysMenuDao.selectRoleMenusByUserCode(userCode);
		//菜单结构树
		List<SysMenuDTO> tree = new ArrayList<SysMenuDTO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(SysMenuDTO menu : list){
				//筛选出所有的一级菜单
				if(StringUtils.equals("-1", menu.getParentMenuCode())){
					//递归结构化子菜单
					List<SysMenuDTO> children = getChildren(menu, list);
					if(CollectionUtils.isNotEmpty(children)){
						menu.setChildren(children);
					}
					tree.add(menu);
				}
			}
		}
		return tree;
	}
	
	@Override
	public List<String> getUserMenuPath() throws MarvelsBusinessException {
		UserDTO user = CacheUtils.getCacheUser();
		String userCode = user.getUserCode();
		List<SysMenuDTO> list = sysMenuDao.selectRoleMenusByUserCode(userCode);
		
		if(CollectionUtils.isEmpty(list)){
			list = new ArrayList<SysMenuDTO>();
		}
				
		List<String> userMenuPathList = new ArrayList<String>();
		for (SysMenuDTO sysMenuDTO : list) {
			userMenuPathList.add(sysMenuDTO.getMenuPath());
		}
		// super用户默认加载启停管理
		if(StrUtil.equals(userCode, MarvelsConstant.SYSTEM_SUPER_CODE)){
			userMenuPathList.add("sys_up_down");
		}
		list.clear();
		
		return userMenuPathList;
	}
	
	//查询当前登录用户有权限访问的菜单集合
	private List<SysMenuDTO> getUserMenuList() throws MarvelsBusinessException {
		UserDTO user = CacheUtils.getCacheUser();
		String userCode = user.getUserCode();
		//角色菜单数据
		List<SysMenuDTO> list = sysMenuDao.selectRoleMenusByUserCode(userCode);
		return list;
	}
	
	private List<SysMenuDTO> getChildren(SysMenuDTO parent, List<SysMenuDTO> list){
		List<SysMenuDTO> children = new ArrayList<SysMenuDTO>();
		for(SysMenuDTO node : list){
			//筛选出父级菜单下的全部子菜单
			if(StringUtils.equals(parent.getMenuCode(), node.getParentMenuCode())){
				children.add(node);
			}
		}
		if(CollectionUtils.isNotEmpty(children)){
			for(SysMenuDTO child : children){
				child.setChildren(getChildren(child, list));
			}
		}
		if(CollectionUtils.isNotEmpty(children)){
			return children;
		}else{
			return null;
		}
	}

	@Override
	public Map<String, String> getPermissionMap(String userCode) {
		List<SysMenuDTO>  list = sysMenuDao.selectPermission(userCode);
		Map<String, String> map = new HashMap<String,String>();
		for(SysMenuDTO sysMenuDTO : list) {
			map.put(sysMenuDTO.getMenuCode(), sysMenuDTO.getMenuCode());
		}
		return map;
	}
	
	@Override
	public Map<String, String> getPermissionUrlMap(String userCode) {
		List<SysMenuDTO>  list = sysMenuDao.selectPermission(userCode);
		Map<String, String> map = new HashMap<String,String>();
		for(SysMenuDTO sysMenuDTO : list) {
			map.put(sysMenuDTO.getMenuUrl(), sysMenuDTO.getMenuUrl());
		}
		return map;
	}

}
